<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="房类编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomTypeCode">
                            <a-input v-model="model.roomTypeCode" placeholder="请输入房类编号" :disabled="disableCode"></a-input>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="房类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomTypeName">
                            <a-input v-model="model.roomTypeName" placeholder="请输入房类名称"></a-input>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="房价（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomPrice">
                            <a-input-number v-model="model.roomPrice" placeholder="请输入房价" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="早餐份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="breakfastNum">
                            <a-input-number v-model="model.breakfastNum" placeholder="请输入早餐份数" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="是否作为钟点房" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isClockRoom">
                            <j-dict-select-tag type="list" v-model="model.isClockRoom" dictCode="is_true"
                                placeholder="请选择是否作为钟点房" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="钟点房房价（元）" :labelCol="labelCol" :wrapperCol="wrapperCol"
                            prop="clockRoomPrice">
                            <a-input-number v-model="model.clockRoomPrice" placeholder="请输入钟点房房价" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="是否作为长包房" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isCoupeRoom">
                            <j-dict-select-tag type="list" v-model="model.isCoupeRoom" dictCode="is_true"
                                placeholder="请选择是否作为长包房" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="长包房房价（元）" :labelCol="labelCol" :wrapperCol="wrapperCol"
                            prop="coupeRoomPrice">
                            <a-input-number v-model="model.coupeRoomPrice" placeholder="请输入长包房房价" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="是否作为婚房" :labelCol="labelCol" :wrapperCol="wrapperCol"
                            prop="isWeddingRoom">
                            <j-dict-select-tag type="list" v-model="model.isWeddingRoom" dictCode="is_true"
                                placeholder="请选择是否作为婚房" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="婚房价格（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weddingRoom">
                            <a-input-number v-model="model.weddingRoom" placeholder="请输入婚房价格" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="允许超订数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weddingRoom">
                            <a-input-number v-model="model.overdeterminedNum" placeholder="请输入允许超订数" style="width: 100%"
                                :step="1" :min="0" />
                        </a-form-model-item>
                    </a-col>
                    <!-- <a-col :span="12">
                        <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isEnable">
                            <j-switch defaultChecked v-model="model.isEnable" :options="['1', '0']"></j-switch>
                        </a-form-model-item>
                    </a-col> -->
                </a-row>
            </a-form-model>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'

export default {
    name: 'RoomTypeForm',
    components: {
    },
    props: {
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            disableCode: false,
            model: {
                overdeterminedNum: 0
            },
            labelCol: {
                xs: { span: 24 },
                sm: { span: 8 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 14 },
            },
            confirmLoading: false,
            validatorRules: {
                roomTypeCode: [{ required: true, message: '请完善内容' }],
                roomTypeName: [{ required: true, message: '请完善内容' }],
                roomPrice: [{ required: true, message: '请完善内容' }],
                breakfastNum: [{ required: true, message: '请完善内容' }],
                isClockRoom: [{ required: true, message: '请完善内容' }],
                clockRoomPrice: [],
                isCoupeRoom: [{ required: true, message: '请完善内容' }],
                coupeRoomPrice: [],
                isWeddingRoom: [{ required: true, message: '请完善内容' }],
                weddingRoom: [],
            },
            url: {
                add: "/room/roomType/add",
                edit: "/room/roomType/edit",
                queryById: "/room/roomType/queryById"
            }
        }
    },
    computed: {
        formDisabled() {
            return this.disabled
        },
    },
    watch: {
        'model.isClockRoom'() {
            if (this.model.isClockRoom === '1') {
                this.validatorRules.clockRoomPrice = [{ required: true, message: '请完善内容' }]
            } else {
                this.validatorRules.clockRoomPrice = []
            }
        },
        'model.isCoupeRoom'() {
            if (this.model.isCoupeRoom === '1') {
                this.validatorRules.coupeRoomPrice = [{ required: true, message: '请完善内容' }]
            } else {
                this.validatorRules.coupeRoomPrice = []
            }
        },
        'model.isWeddingRoom'() {
            if (this.model.isWeddingRoom === '1') {
                this.validatorRules.weddingRoom = [{ required: true, message: '请完善内容' }]
            } else {
                this.validatorRules.weddingRoom = []
            }
        }
    },
    created() {
        //备份model原始值
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
        add() {
            this.edit(this.modelDefault);
        },
        edit(record) {
            if (record.roomTypeCode) {
                this.disableCode = true
            }
            this.model = Object.assign({}, record);
            this.visible = true;
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.$refs.form.validate(valid => {
                if (valid) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    httpAction(httpurl, this.model, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }

            })
        },
    }
}
</script>